import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
	state: () => ({
		userInfo: {
			id: null,
			username: '',
			role: 'guest', // guest, user, admin
			avatar: '/static/logo.png',
			isLogin: false,
			email: '',
			phone: '',
			registerTime: null
		},
		users: [] // 存储所有注册用户
	}),

	getters: {
		isAdmin: (state) => state.userInfo.role === 'admin',
		isUser: (state) => state.userInfo.role === 'user',
		isGuest: (state) => state.userInfo.role === 'guest',
		isLoggedIn: (state) => state.userInfo.isLogin
	},

	actions: {
		// 初始化用户数据
		initUserData() {
			// 从本地存储加载用户信息
			const userInfo = uni.getStorageSync('userInfo')
			if (userInfo) {
				this.userInfo = { ...this.userInfo, ...userInfo }
			}

			// 从本地存储加载所有用户
			const users = uni.getStorageSync('users')
			if (users) {
				this.users = users
			} else {
				// 初始化默认管理员账户
				this.users = [
					{
						id: 1,
						username: 'admin',
						password: 'admin123',
						role: 'admin',
						email: 'admin@movie.com',
						phone: '13800138000',
						avatar: '/static/logo.png',
						registerTime: new Date().toISOString()
					}
				]
				this.saveUsersToStorage()
			}
		},

		// 用户登录
		login(username, password) {
			const user = this.users.find(u => u.username === username && u.password === password)
			
			if (user) {
				this.userInfo = {
					id: user.id,
					username: user.username,
					role: user.role,
					avatar: user.avatar,
					isLogin: true,
					email: user.email,
					phone: user.phone,
					registerTime: user.registerTime
				}
				this.saveUserInfoToStorage()
				return { success: true, message: '登录成功' }
			} else {
				return { success: false, message: '用户名或密码错误' }
			}
		},

		// 用户注册
		register(userData) {
			// 检查用户名是否已存在
			const existingUser = this.users.find(u => u.username === userData.username)
			if (existingUser) {
				return { success: false, message: '用户名已存在' }
			}

			// 检查邮箱是否已存在
			const existingEmail = this.users.find(u => u.email === userData.email)
			if (existingEmail) {
				return { success: false, message: '邮箱已被注册' }
			}

			// 创建新用户
			const newUser = {
				id: this.users.length + 1,
				username: userData.username,
				password: userData.password,
				role: 'user',
				email: userData.email,
				phone: userData.phone || '',
				avatar: '/static/logo.png',
				registerTime: new Date().toISOString()
			}

			this.users.push(newUser)
			this.saveUsersToStorage()
			
			return { success: true, message: '注册成功' }
		},

		// 游客登录
		guestLogin() {
			this.userInfo = {
				id: 0,
				username: '游客用户',
				role: 'guest',
				avatar: '/static/logo.png',
				isLogin: true,
				isGuest: true,
				email: '',
				phone: '',
				registerTime: null
			}
			this.saveUserInfoToStorage()
		},

		// 用户登出
		logout() {
			this.userInfo = {
				id: null,
				username: '',
				role: 'guest',
				avatar: '/static/logo.png',
				isLogin: false,
				email: '',
				phone: '',
				registerTime: null
			}
			uni.removeStorageSync('userInfo')
		},

		// 保存用户信息到本地存储
		saveUserInfoToStorage() {
			uni.setStorageSync('userInfo', this.userInfo)
		},

		// 保存所有用户到本地存储
		saveUsersToStorage() {
			uni.setStorageSync('users', this.users)
		},

		// 更新用户信息
		updateUserInfo(newInfo) {
			this.userInfo = { ...this.userInfo, ...newInfo }
			this.saveUserInfoToStorage()
			
			// 同时更新users数组中的用户信息
			const userIndex = this.users.findIndex(u => u.id === this.userInfo.id)
			if (userIndex !== -1) {
				this.users[userIndex] = { ...this.users[userIndex], ...newInfo }
				this.saveUsersToStorage()
			}
		}
	}
})
